package com.lanou.lenanapp.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.lanou.lenanapp.R;
import com.lanou.lenanapp.adapter.CollectAdapter;
import com.lanou.lenanapp.db.DatabaseHelper;
import com.lanou.lenanapp.entity.greendao.CollectEntity;

import java.util.List;

/**
 * Created by dllo on 15/11/5.
 */
public class JokeCollectFragment extends Fragment implements CollectAdapter.OnRemoveClickListener {


    private RecyclerView collectrecyclerView;
    private GridLayoutManager manager;
    private CollectAdapter adapter;
    private DatabaseHelper helper;
    private Context context;
    private TextView goneTv;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_collect_joke, null);
        collectrecyclerView = (RecyclerView) v.findViewById(R.id.collectrecyclerView);
        goneTv = (TextView) v.findViewById(R.id.goneTv);//跑马灯
        goneTv.setSelected(true);
        goneTv.requestFocus();
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        helper = DatabaseHelper.getHelper();

        manager = new GridLayoutManager(context, 1);
        collectrecyclerView.setLayoutManager(manager);
        adapter = new CollectAdapter(context);
        adapter.setOnRemoveClickListener(this);
        collectrecyclerView.setAdapter(adapter);



        //取collect数据库中的数据
        List<CollectEntity> collectEntities = helper.queryAllCollect();
        //将数据添加到适配器
        if (collectEntities != null && collectEntities.size() > 0){
            adapter.addData(collectEntities);
        }else {
            goneTv.setVisibility(View.VISIBLE);
        }
    }

    //回调接口 判断收藏里是否有东西 如果是空的 则显示控件
    @Override
    public void OnRemoveClick(List<CollectEntity> list) {
        if (list == null || list.size() == 0){
            goneTv.setVisibility(View.VISIBLE);
        }
    }

}
